package com.ruoyi.api.controller;


import cn.hutool.json.JSONObject;
import com.ruoyi.api.dto.DeviceDto;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.erecord.device.domain.Device;
import com.ruoyi.erecord.device.domain.vo.DeviceVo;
import com.ruoyi.erecord.device.service.IDeviceService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RequiredArgsConstructor
@RestController
@RequestMapping("/api/unauth/device")
public class ApiDeviceController {


    private final IDeviceService iDeviceService;
    /**
     * 获取用户设备数据信息
     */
    @PostMapping("/queryDeviceInfo")
    public R<JSONObject> queryDeviceInfo(@RequestBody JSONObject _params) {
        List<DeviceDto> ps =_params.getJSONArray("user_list").toList(DeviceDto.class);
        List<Device> params = new ArrayList<>();
        for(DeviceDto item:ps){
            Device d = new Device();
            d.setName(item.getUser_name());
            d.setPhone(item.getPhone());
            params.add(d);
        }
        List<Device> list = iDeviceService.queryByList(params);
        List<DeviceDto> dtos = new ArrayList<>();
        for(Device item :list){
            DeviceDto dto = new DeviceDto();
            dto.setUser_name(item.getName());
            dto.setPhone(item.getPhone());
            dto.setGps_device_id(item.getCode());
            dtos.add(dto);
        }
        JSONObject obj = new JSONObject();
        obj.set("user_gps_list",dtos);
        return R.ok(obj);
    }
}
